﻿using gptDataType.Helper;
using gptDataType.Models;
using gptDataType.Type;
using gptDataType.Type.bit;
using gptDataType.Type.combination;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace gptDataType.ViewModels
{
    public class MainWindowViewModel
    {
        [Reactive]
        public string Title { get; set; } = "gptDataType";

        [Reactive]
        public string GeneratedCode { get; set; } = "C# Code";

        PlcVariableTable table { get; set; }

        public ObservableCollection<PlcVariableViewModel> Variables { get; }
       = new ObservableCollection<PlcVariableViewModel>();


        public MainWindowViewModel()
        {
            // 创建示例数据
            table = CreateSampleData();
            table.compileAddr();
          
            LoadData(table);


            var test = table.GetAllVariables();

            CodeGenerator codeGenerator = new CodeGenerator(table);
            
            GeneratedCode = codeGenerator.GenerateCode();
        }

        private void LoadData(PlcVariableTable table)
        {
            var test = table.GetMembers();
            foreach (var member in table.GetMembers())
            {
                Variables.Add(new PlcVariableViewModel(member));
            }
        }
        private PlcVariableTable CreateSampleData()
        {
            //提供所需的参数以修复 CS7036 错误
           var table = new PlcVariableTable("SampleTable", DataType.Struct, 0, null);
            //table.AddVariable(new PlcBool("StartButton", false));
            //table.AddVariable(new PlcBool("EndButton", true));
            //table.AddVariable(new PlcDWord("Counter", 0x12345678));
            //table.AddVariable(new PlcDate("ProductionDate", new DateTime(2023, 10, 1)));

            // 创建 Word 数组  
            var arr = new PlcArray("word数组", new PlcWord("word", 0x123), 10);
            table.AddVariable(arr);

            //// 创建结构体  
            //var motor = new PlcStruct("Motor1");
            //motor.AddMember(new PlcBool("Enabled", true));
            //motor.AddMember(new PlcDWord("RPM", 1500));
            //motor.AddMember(new PlcBool("testButton", false));
            //motor.AddMember(new PlcBool("testButton2", false));
            //var motor2 = new PlcStruct("Motor2");

            //motor2.AddMember(new PlcBool("Enabled1", true));
            //motor2.AddMember(new PlcBool("Enabled2", true));

            //motor2.AddMember(new PlcBool("Enabled3", true));
            //motor.AddMember(motor2);

            //// 创建结构体数组  
            //var arr1 = new PlcArray("结构体数组", motor2, 5);
            //table.AddVariable(arr1);

            //motor.AddMember(new PlcDWord("RPM1", 1500));

            //table.AddVariable(motor);


            //// 结构体中添加数组，数组中添加结构体  
            //var motor3 = new PlcStruct("结构体套数组");
            //var arr2 = new PlcArray("数组套结构体", motor2, 5);
            //motor3.AddMember(arr2);
            //motor3.AddMember(new PlcBool("Enabled", true));
            //table.AddVariable(motor3);
            //table.AddVariable(new PlcBool("SsButton", false));
            ////table.AddVariable(new PlcBool("SsButton", false));
            // 创建嵌套结构体
            var motor4 = new PlcStruct("Motor4");
            motor4.AddMember(new PlcBool("EnabledMotor4", true));
            var motor5 = new PlcStruct("Motor5");
            motor5.AddMember(new PlcBool("EnabledMotor5", true));
            var motor6 = new PlcStruct("Motor6");
            motor6.AddMember(new PlcBool("EnabledMotor6", true));
            motor4.AddMember(motor5);
            motor5.AddMember(motor6);
            table.AddVariable(motor4);

            //#region 解析json
            // 指定文件夹路径
            //string folderPath = @"D:\work\2025-04-Tools\西门子xml\PVW2010_xmltestuse";
            //CompareJson compareJson = new CompareJson(folderPath);
            //try
            //{
            //    // 调用方法获取所有 .json 文件的全路径列表

            //    List<string> jsonFilePaths = compareJson.jsonFiles;

            //    // 打印结果
            //    Debug.WriteLine("JSON 文件路径：");
            //    if (jsonFilePaths.Count > 0)
            //    {
            //        foreach (string filePath in jsonFilePaths)
            //        {
            //            Debug.WriteLine(filePath);
            //        }
            //    }
            //    else
            //    {
            //        Debug.WriteLine("没有找到任何 .json 文件。");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Debug.WriteLine($"发生错误：{ex.Message}");
            //}
            //compareJson.ReadJsonFiles();
            //#endregion
            #region 解析xml
            string folderPathxml = @"D:\work\2025-04-Tools\西门子xml\PVW2010_xmltestuse";
            //string folderPathxml = @"D:\work\2025-04-Tools\西门子xml\C#tt";
            //ParseXmzXml parseXmz = new ParseXmzXml(folderPathxml);
            //parseXmz.ParseXmlFiles();
            ParseXmz2SelfDataStruct parseXmz2SelfData = new(folderPathxml);
            parseXmz2SelfData.StartParse();

            #endregion
            Debug.WriteLine(parseXmz2SelfData.allCammand.ToString());
            return table;
        }
    }
}
